/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import com.eelpo.common.util.BlankUtils;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * <pre>
 * 树结构实体，用于映射T_TREE_STRUCT表(Persist Object)
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Entity
@Table(name = "T_ENTITY_RELATION")
public class TEntityRelation implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7925499662388819211L;

	/**
	 * 结构Id
	 */
	@NotEmpty
	private String id;

	/**
	 * 深度
	 */
	@NotNull
	private Integer depth;

	/**
	 * 父实体
	 */
	@NotNull
	private TEntity parentEntity;

	/**
	 * 子实体
	 */
	@NotNull
	private TEntity childEntity;

	public TEntityRelation() {

	}

	public TEntityRelation(String id) {
		this.id = id;
	}

	public TEntityRelation(TEntity parentEntity, TEntity childEntity, Integer depth) {
		this.depth = depth;
		this.childEntity = childEntity;
		this.parentEntity = parentEntity;
	}

	@Id
	@Column(length = 36)
	@GenericGenerator(name = "uuid", strategy = "uuid2")
	@GeneratedValue(generator = "uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(nullable = false)
	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parentId", nullable = false)
	public TEntity getParentEntity() {
		return parentEntity;
	}

	public void setParentEntity(TEntity parentEntity) {
		this.parentEntity = parentEntity;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "childId", nullable = false)
	public TEntity getChildEntity() {
		return childEntity;
	}

	public void setChildEntity(TEntity childEntity) {
		this.childEntity = childEntity;
	}

	@Override
	public boolean equals(Object obj) {
		if (BlankUtils.isBlank(obj)) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof TEntityRelation)) {
			return false;
		}

		TEntityRelation relation = (TEntityRelation) obj;

		if (BlankUtils.isBlank(this.id) || BlankUtils.isBlank(relation.getId())) {
			return false;
		}

		return this.id.equals(relation.getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "TEntityRelation[id=" + id + "]";
	}

}